package com.xian.im.modules.avchat.rule.handler;

import com.xian.im.common.constant.CommandCodeConstant;
import com.xian.im.common.constant.CommandConstant;
import com.xian.im.common.exception.BusinessException;
import com.xian.im.common.model.session.SessionGroup;
import com.xian.im.common.model.session.SessionUser;
import com.xian.im.common.proto.MsgRequest;
import com.xian.im.common.utils.bussiness.GroupContextUtil;
import com.xian.im.modules.avchat.rule.CheckMute;
import com.xian.im.modules.im.core.IGroupCoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @program: xian-code
 * @description: 禁言 Checking
 * @author: liru.xian
 * @create: 2021-01-21 16:38
 **/
@Slf4j
@Component
public class GroupCheckMute implements CheckMute {

    @Autowired
    private IGroupCoreService groupCoreService;
    /**
     *  是否禁言  member
     * @param message
     * @return
     */
    @Override
    public void mute(MsgRequest message, SessionUser sessionUser){
        log.debug("群组禁言");
        if(message.getCmd().equals(CommandConstant.GROUP_CMD)){
            if(message.getCmdCode().equals(CommandCodeConstant.GROUP_SEND)){
                SessionGroup sessionGroup = GroupContextUtil.getSessionGroup(message.getGroupUniqueId());
                Boolean flag = true;
                if (sessionGroup != null) {
                    Integer shutUp = sessionGroup.getShutUp();
                    flag  = 0 == shutUp ? Boolean.TRUE : Boolean.FALSE;
                }else {
                    flag = groupCoreService.groupMute(message.getGroupUniqueId());
                }
                if(!flag){
                    throw new BusinessException("群组禁言");
                }
            }
        }
    }
}
